<template>
  <view>
    <!-- 搜索组件 -->
    <my-search @click="goToSearch" />
    <view class="scroll-view-container">
      <!-- 左边一级分类 -->
      <scroll-view
        scroll-y
        class="left-scroll-view"
        :style="{ height: h + 'px' }"
      >
        <view
          :class="[
            'left-scroll-view-item',
            currIndex === index ? 'active' : ''
          ]"
          v-for="(item, index) in cateList"
          @click="toggle(index)"
          :key="item.cat_id"
        >
          {{ item.cat_name }}
        </view>
      </scroll-view>
      <!-- 右边二级、三级 -->
      <scroll-view
        scroll-y
        class="right-scroll-view"
        :style="{ height: h + 'px' }"
        :scroll-top="scrollTop"
      >
        <view v-for="item in cateLevel2" :key="item.cat_id">
          <view class="cate-lv2-title">/ {{ item.cat_name }} /</view>
          <view class="cate-lv3-list">
            <view
              v-for="subitem in item.children"
              :key="subitem.cat_id"
              class="cate-lv3-item"
              @click="goToGoodsList(subitem)"
            >
              <image :src="subitem.cat_icon" mode="" />
              <text>{{ subitem.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request'
import MySearch from '@/components/my-search'
export default {
  components: {
    MySearch
  },
  data () {
    return {
      // 分类数据列表
      cateList: [],
      cateLevel2: [],
      currIndex: 0,
      h: 0, // 两边scroll-view的高度
      scrollTop: 0 // 右边scroll-view距离顶部的距离
    }
  },
  onLoad () {
    this.getCateList()

    this.h = uni.getSystemInfoSync().windowHeight - 50
  },
  methods: {
    async getCateList () {
      const res = await request({
        url: '/api/public/v1/categories'
      })

      if (res.meta.status === 200) {
        this.cateList = res.message
        this.cateLevel2 = res.message[this.currIndex].children
      }
    },
    toggle (index) {
      this.currIndex = index

      this.cateLevel2 = this.cateList[this.currIndex].children

      this.scrollTop = this.scrollTop === 0 ? 1 : 0
    },
    // 跳转到商品列表
    goToGoodsList (item) {
      uni.navigateTo({
        url: '/subpkg/goods_list/index?cat_id=' + item.cat_id
      })
    },
    // 跳转到搜索页面
    goToSearch () {
      uni.navigateTo({
        url: '/subpkg/search/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .right-scroll-view {
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
}
</style>
